//
//  AddFreeViewController.m
//  DaJiang
//
//  Created by sally on 16/10/6.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "AddFreeViewController.h"
#import "CellManager.h"

@interface AddFreeViewController () 

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *tableData;
@property (strong, nonatomic) UITextField *timeTextField;
@property (strong, nonatomic) UITextField *locTextField;

@end

@implementation AddFreeViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self initControl];
  [self initData];
}

#pragma mark 初始化控件
- (void)initControl {
  self.title = @"添加";
  self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(gotoAdd)];
  [[CommonMethod sharedCommonMethod] setNavButtonItem:self.navigationItem.rightBarButtonItem type:ButtonItemType_Orange];
  [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

#pragma mark 初始化数据
- (void)initData {
  self.tableData = [NSMutableArray array];
  [self.tableData addObject:@[ @"时间" ]];
  [self.tableData addObject:@[ @"地点" ]];
}

#pragma mark - TableView data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return self.tableData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return [[self.tableData objectAtIndex:section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  return 0.1; //section头部高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  return GNormalMargin; //section底部高度
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell;
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

  //向右箭头，列表项点击性
  cell.accessoryType = UITableViewCellAccessoryNone;
  cell.selectionStyle = UITableViewCellSelectionStyleNone;

  //编辑行
  UITextField *textField = [CellManager configCellNormalTextField:cell];
  textField.tag = indexPath.section;
  [cell.contentView addSubview:textField];
  if (indexPath.section == 0) {
    self.timeTextField = textField;
  }else{
    self.locTextField = textField;
  }

  //主标题
  [CellManager configCellNormalText:cell];
  cell.textLabel.text = [[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

  return cell;
}

#pragma mark TableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView deselectRowAtIndexPath:indexPath animated:YES]; //返回后选中状态消失
}

#pragma mark - 私有方法
#pragma mark 添加完成
- (void)gotoAdd {
  if (self.timeTextField.text.length < 1) {
    [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"请填入时间"];
    return;
  }
  if (self.locTextField.text.length < 1) {
    [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"请填入地点"];
    return;
  }
  
  MBProgressHUD *hud = [[CommonMethod sharedCommonMethod] createProgressHUDInView:self.view withMessage:@""];
  
  [FreeModel addFreeWithTime:self.timeTextField.text location:self.locTextField.text completion:^(FreeModel *freeModel) {
    if (freeModel) {
      //刷新有空列表数据
      if (self.delegate && [self.delegate respondsToSelector:@selector(refreshFreeListVc)]) {
        [self.delegate refreshFreeListVc];
      }
      [self.navigationController popViewControllerAnimated:YES];
    } else {
      NSError *error = [FreeModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
    hud.hidden = YES;
  }];
}

@end
